package com.dataextend.service;


import com.alibaba.fastjson.JSONObject;
import com.dataextend.common.utils.CommonUtil;
import com.dataextend.common.utils.Constants;
import com.dataextend.common.utils.ErrorEnum;
import com.dataextend.dto.Logindto;
import com.dataextend.entity.SysRoleMybatis;
import com.dataextend.entity.SysUserMybatis;
import com.dataextend.mapper.cluster.SysUserMapper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    /**
     *  登录表单提交
     */
    public JSONObject authLogin(Logindto logindto){
        String username = logindto.getUsername();
        String password = logindto.getPassword();
        JSONObject info = new JSONObject();
        Subject currentUser = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        System.out.println("---------------------" + username + " " + password);
        token.setRememberMe(true);
        try {
            currentUser.login(token);
            info.put("result", "success");
        } catch (AuthenticationException e){
            info.put("result", "fail");
        }
        return CommonUtil.successJson(info);
    }


    /**
     *  根据用户名密码查询对应的用户
     */
    public JSONObject getUser(String username, String password){
        return sysUserMapper.getUser(username,password);
    }


    /**
     * 退出登录
     */
    public JSONObject logout(){
        try {
            Subject currentUser = SecurityUtils.getSubject();
            currentUser.logout();
        } catch (Exception e){
            System.out.println(" ");
        }
        return CommonUtil.successJson();
    }


    /**
     * 查询当前登录用户的权限等信息
     */
    public JSONObject getInfo(){
        // 从session获取用户信息
        Session session =  SecurityUtils.getSubject().getSession();
        JSONObject userInfo = (JSONObject) session.getAttribute(Constants.SESSION_USER_INFO);
        String username = userInfo.getString("username");
        JSONObject info = new JSONObject();
        JSONObject userPermission = sysUserMapper.getUserPermission(username);
        session.setAttribute(Constants.SESSION_USER_PERMISSION, userPermission);
        info.put("userPermission", userPermission);
        return CommonUtil.successJson(info);
    }


    /**
     * 用户列表
     */
    public JSONObject listUser(Integer offset, Integer pageRow){
        int count=10;
        System.out.println("---------------" + offset + " " + pageRow);
        List<JSONObject> list = sysUserMapper.listUser(offset, pageRow);
        return CommonUtil.successPage(pageRow, offset, list, count);
    }


    /**
     * 添加用户
     */
    public JSONObject addUser(SysUserMybatis sysUserMybatis){
        sysUserMapper.addUser(sysUserMybatis);
        return CommonUtil.successJson();
    }


    /**
     * 修改用户
     */
    public JSONObject updateUser(SysUserMybatis sysUserMybatis){
        sysUserMapper.updateUser(sysUserMybatis);
        return CommonUtil.successJson();
    }


    /**
     * 角色列表
     */
    public JSONObject listRole(){
        List<JSONObject> roles = sysUserMapper.listRole();
        return CommonUtil.successPage(roles);
    }


    /**
     * 查询所有权限,给角色分配权限时调用
     */
    public JSONObject listAllPermission(){
        List<JSONObject> permission = sysUserMapper.listAllPermission();
        return CommonUtil.successPage(permission);
    }


    /**
     * 添加角色
     */
    public JSONObject addRole(SysRoleMybatis sysRoleMybatis){
        sysUserMapper.insertRole(sysRoleMybatis.getRole_name());
        sysUserMapper.insertRolePermission(sysRoleMybatis.getRole_id(),(List<Integer>) sysRoleMybatis.getPermission_id());
        return CommonUtil.successJson();
    }


    /**
     * 删除角色
     */
    public JSONObject deleteRole(SysRoleMybatis sysRoleMybatis){
        JSONObject roleInfo = sysUserMapper.getRoleAllInfo(sysRoleMybatis.getRole_id());
        List<JSONObject> users = (List<JSONObject>) roleInfo.get("users");
        if (users != null && users.size() > 0) {
            return CommonUtil.errorJson(ErrorEnum.E_10008);
        }

        sysUserMapper.removeRole(sysRoleMybatis.getRole_id());
        sysUserMapper.removeRoleAllPermission(sysRoleMybatis.getRole_id());
        return CommonUtil.successJson();
    }

}
